#########################################################################
#			xenconfigutils.py : Xen config file utilities				#
#																		#
#########################################################################

#!/usr/bin/env python

	
# Filter out vars which are not useful for a config
def FilterCfgVars(vmDict):

	# Create list of unwanted keys for the filter function
	unwantedKeys = []
	
	# Add domainCfg keys
	unwantedKeys.append('ssidref')
	unwantedKeys.append('cpu_time')
	unwantedKeys.append('handle')
	unwantedKeys.append('vm')
	unwantedKeys.append('domid')
	unwantedKeys.append('running')
	unwantedKeys.append('cpu')
	unwantedKeys.append('cpu_weight')
	unwantedKeys.append('vcpu_avail')
	unwantedKeys.append('online_vcpus')
	unwantedKeys.append('maxmem')
	unwantedKeys.append('start_time')
	
	# Create a new dictionary to hold the results
	newDict = {}		
	
	# Iterate over vmDict
	for key, val in vmDict.items():
		
		# Make sure the key is not unwanted and the value is not blank
		if key not in unwantedKeys and val != '' and val != '0':
		
			# Add
			newDict[key] = val
	
	# Return
	return newDict

# Writes regular vars to a file in key, value pairs
def WriteKeyValCfgVars(cfgDict, cfgFile):
	
	# Iterate over all config vars
	for key, val in cfgDict.items():
		
		# Write digits without quotes
		if val.isdigit():
			cfgFile.write(key + ' = ' + val + '\n')
		
		# Write strings with quotes
		else:
			cfgFile.write(key + ' = ' + '"' + val + '"' + '\n')
		
def WriteImageCfgVars(myVM, cfgFile):

	# Write kernel line
	cfgFile.write('kernel = ' + '"' + myVM.imageCfgVars['kernel'] + '"' + '\n')
	
	# Write randisk line
	cfgFile.write('ramdisk = ' + '"' + myVM.imageCfgVars['ramdisk'] + '"' + '\n')
	
	# Parse root out of command line
	myRoot = myVM.GetCfgRootField()
	
	# Write root if we got one
	if myRoot != '':
		cfgFile.write('root = ' + myRoot + '\n')
	
# Writes networkCfg vars to a file
def WriteNetworkCfgVars(myVM, cfgFile):

	# Write dhcp line
	cfgFile.write('dhcp = "dhcp"\n')
	
	# Write hostname line
	cfgFile.write('hostname = "' + myVM.GetHostname() + '"\n')
	
	# Iterate over all VIF's
	for vif in myVM.networkCfgVars:
	
		# Write the vif line
		cfgFile.write('vif = ["mac=' + vif['mac'] + ',bridge=' + vif['bridge'] + '"]' + '\n')
	
# Writes vbdCfg vars to a file
def WriteVbdCfgVars(myVM, cfgFile):
	
	# Create list to hold disk strings
	diskStrings = []
	
	# Iterate over VBDs
	for cfgDict in myVM.vbdCfgVars:
		diskStrings.append("'" + cfgDict['type'] + ":" + cfgDict['params'] + "," + cfgDict['dev'] + "," + cfgDict['mode'] + "'")
	
	# Write disk line
	cfgFile.write('disk = [')
	for diskString in diskStrings:
		
		# Write this disk
		cfgFile.write(diskString)
		
		# Write comma if necessary
		if diskStrings.index(diskString) != (len(diskStrings) - 1):
			cfgFile.write(', ')
		
	cfgFile.write(']\n')
	

# Builds and writes a config file base on a VM object
def WriteConfig(cfgPath, myVM):

	# Open the config file
	cfgFile = open(cfgPath, 'w')
	
	# Write imageCfg vars
	WriteImageCfgVars(myVM, cfgFile)
	
	# Filter domainCfg vars
	filteredDomainCfgVars = FilterCfgVars(myVM.domainCfgVars)
	
	# Write domainCfg vars
	WriteKeyValCfgVars(filteredDomainCfgVars, cfgFile)
	
	# Write networkCfg vars
	WriteNetworkCfgVars(myVM, cfgFile)
	
	# Write vbdCfg vars
	WriteVbdCfgVars(myVM, cfgFile)
	
	# Close the config file
	cfgFile.close()
